package com.xunk.system.controller;

import com.xunk.common.core.utils.poi.ExcelUtil;
import com.xunk.common.core.web.controller.BaseController;
import com.xunk.common.core.web.domain.AjaxResult;
import com.xunk.common.core.web.page.TableDataInfo;
import com.xunk.common.log.annotation.Log;
import com.xunk.common.log.enums.BusinessType;
import com.xunk.common.security.utils.SecurityUtils;
import com.xunk.system.domain.SysBimModel;
import com.xunk.system.service.ISysBimModelService;
import com.xunk.system.vo.modelBindVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 模型Controller
 *
 * @author ruoyi
 * @date 2021-03-10
 */
@RestController
@RequestMapping("/model")
public class SysBimModelController extends BaseController {
    @Autowired
    private ISysBimModelService sysBimModelService;

    /**
     * 查询模型列表
     */
    @PreAuthorize("@ss.hasPermi('buz:model:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBimModel sysBimModel) {
        startPage();
        List<SysBimModel> list = sysBimModelService.selectSysBimModelList(sysBimModel);
        return getDataTable(list);
    }

    /**
     * 导出模型列表
     */
    @PreAuthorize("@ss.hasPermi('buz:model:export')")
    @Log(title = "模型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBimModel sysBimModel) throws IOException {
        List<SysBimModel> list = sysBimModelService.selectSysBimModelList(sysBimModel);
        ExcelUtil<SysBimModel> util = new ExcelUtil<SysBimModel>(SysBimModel.class);
        util.exportExcel(response, list, "model");
    }

    /**
     * 获取模型详细信息
     */
    @PreAuthorize("@ss.hasPermi('buz:model:query')")
    @GetMapping(value = "/{modelId}")
    public AjaxResult getInfo(@PathVariable("modelId") Long modelId) {
        return AjaxResult.success(sysBimModelService.selectSysBimModelById(modelId));
    }

    /**
     * 新增模型
     */
    @PreAuthorize("@ss.hasPermi('buz:model:add')")
    @Log(title = "模型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBimModel sysBimModel) {
        sysBimModel.setCreateBy(SecurityUtils.getUsername());
        return toAjax(sysBimModelService.insertSysBimModel(sysBimModel));
    }

    /**
     * 修改模型
     */
    @PreAuthorize("@ss.hasPermi('buz:model:edit')")
    @Log(title = "模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBimModel sysBimModel) {
        sysBimModel.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(sysBimModelService.updateSysBimModel(sysBimModel));
    }

    /**
     * 删除模型
     */
    @PreAuthorize("@ss.hasPermi('buz:model:remove')")
    @Log(title = "模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{modelIds}")
    public AjaxResult remove(@PathVariable Long[] modelIds) {
        return toAjax(sysBimModelService.deleteSysBimModelByIds(modelIds));
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
//    @PreAuthorize("@ss.hasPermi('buz:model:upload')")
    @Log(title = "上传模型", businessType = BusinessType.OTHER)
    @PostMapping("/upload")
    public AjaxResult update(MultipartFile file) throws IOException {
        return AjaxResult.success(sysBimModelService.uploadModelFile(file));
    }

    @PreAuthorize("@ss.hasPermi('buz:model:bind')")
    @Log(title = "绑定模型", businessType = BusinessType.UPDATE)
    @PutMapping("/bind")
    public AjaxResult bingModel(@RequestBody modelBindVO vo){
        sysBimModelService.bindModelAndDept(vo.getModelId(),vo.getDeptIds());
        return AjaxResult.success();
    }
}
